package com.zk.nakedfowl.basicData.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.zk.nakedfowl.basicData.dao.TerminalDao;
import com.zk.nakedfowl.basicData.pojo.Card;
import com.zk.nakedfowl.basicData.pojo.Terminal;
import com.zk.nakedfowl.common.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class TerminalService {

	@Autowired
	private TerminalDao terminalDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Terminal> findAll() {
		return terminalDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Terminal> findSearch(Map whereMap, int page, int size) {
		Specification<Terminal> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return terminalDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Terminal> findSearch(Map whereMap) {
		Specification<Terminal> specification = createSpecification(whereMap);
		return terminalDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Terminal findById(String id) {
		return terminalDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param terminal
	 */
	public void add(Terminal terminal) {
		terminal.setId( idWorker.nextId()+"" );
		terminalDao.save(terminal);
	}

	/**
	 * 修改
	 * @param terminal
	 */
	public void update(Terminal terminal) {
		terminalDao.save(terminal);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		terminalDao.deleteById(id);
	}


	public Object findByIdName(String id, String createBy) {

		Map<String,String> m=new HashMap();
		m.put("id",id);
		m.put("createBy",createBy);

		Specification<Terminal> specification = createSpecification(m);
		return terminalDao.findAll(specification);

	}




	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Terminal> createSpecification(Map searchMap) {

		return new Specification<Terminal>() {

			@Override
			public Predicate toPredicate(Root<Terminal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.equal(root.get("id").as(String.class), (String)searchMap.get("id")));
                }
                // 销售终端编号
                if (searchMap.get("number")!=null && !"".equals(searchMap.get("number"))) {
                	predicateList.add(cb.equal(root.get("number").as(String.class), (String)searchMap.get("number")));
                }
                // 名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.equal(root.get("name").as(String.class), (String)searchMap.get("name")));
                }
                // 地址
                if (searchMap.get("addr")!=null && !"".equals(searchMap.get("addr"))) {
                	predicateList.add(cb.like(root.get("addr").as(String.class), "%"+(String)searchMap.get("addr")+"%"));
                }
                // 负责人
                if (searchMap.get("responsiblePerson")!=null && !"".equals(searchMap.get("responsiblePerson"))) {
                	predicateList.add(cb.equal(root.get("responsiblePerson").as(String.class), (String)searchMap.get("responsiblePerson")));
                }
                // 手机
                if (searchMap.get("mobile")!=null && !"".equals(searchMap.get("mobile"))) {
                	predicateList.add(cb.like(root.get("mobile").as(String.class), "%"+(String)searchMap.get("mobile")+"%"));
                }
                // 电话
                if (searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))) {
                	predicateList.add(cb.like(root.get("phone").as(String.class), "%"+(String)searchMap.get("phone")+"%"));
                }
                // 邮箱
                if (searchMap.get("email")!=null && !"".equals(searchMap.get("email"))) {
                	predicateList.add(cb.like(root.get("email").as(String.class), "%"+(String)searchMap.get("email")+"%"));
                }
                // 创建者
                if (searchMap.get("create_by")!=null && !"".equals(searchMap.get("create_by"))) {
                	predicateList.add(cb.equal(root.get("create_by").as(String.class), (String)searchMap.get("create_by")));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
